package com.android.functionmanagement.ui;

import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

import com.android.functionmanagement.BasePreferenceActivity;
import com.android.functionmanagement.service.AlertService;
import com.uwatch.helowin.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * A {@link PreferenceActivity} that presents a set of application settings. On
 * handset devices, settings are presented as a single list. On tablets,
 * settings are split by category, with category headers shown to the left of
 * the list of settings.
 * <p>
 * See <a href="http://developer.android.com/design/patterns/settings.html">
 * Android Design: Settings</a> for design guidelines and the <a
 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
 * API Guide</a> for more information on developing a Settings UI.
 */
public class AlertPharmacySettings extends BasePreferenceActivity implements
		OnPreferenceChangeListener {
	/**
	 * Determines whether to always show the simplified settings UI, where
	 * settings are presented in a single list. When false, settings are shown
	 * as a master/detail two-pane view on tablets. When true, a single pane is
	 * shown on tablets.
	 */
	public static final String PH_RING_ONOFF = "PH_RING_ONOFF";
	public static final String PH_VIBRATE_ONOFF = "PH_VIBRATE_ONOFF";
	public static final String PH_TIME_PICK = "PH_TIME_PICK";
	public static final String PH_DAY_PICK = "PH_DAY_PICK";
	private EditTextPreference mTextPreference;
	private MultiSelectListPreference mSelectListPreference;

	@SuppressWarnings("deprecation")
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		startService(new Intent(this, AlertService.class));
		addPreferencesFromResource(R.xml.alert_ph_settings);
		init();
	}

	/**
	 * 
	 */
	private void init() {
		// TODO Auto-generated method stub
		findPreference(PH_RING_ONOFF).setOnPreferenceChangeListener(this);
		findPreference(PH_VIBRATE_ONOFF).setOnPreferenceChangeListener(this);

		
		mTextPreference = (EditTextPreference) findPreference(PH_TIME_PICK);
		mTextPreference.setOnPreferenceChangeListener(this);
		mSelectListPreference = (MultiSelectListPreference) findPreference(PH_DAY_PICK);
		mSelectListPreference.setOnPreferenceChangeListener(this);

		boolean firstLaunch = PreferenceManager.getDefaultSharedPreferences(
				this).getBoolean("PH_FIRST_LAUNCH", true);
		String txt = mTextPreference.getText();
		if (txt == null && firstLaunch) {
			txt = getString(R.string.def_ph_time);
			mTextPreference.setText(txt);
		}
		mTextPreference.setTitle(getString(R.string.alert_time) + txt);
		Set<String> setValues = mSelectListPreference.getValues();
		if (setValues.size() == 0 && firstLaunch) {
			Collections.addAll(setValues,
					getResources().getStringArray(R.array.day_value));
			mSelectListPreference.setValues(setValues);
		}
		List<String> list = new ArrayList<String>(setValues);
		Collections.sort(list);
		CharSequence[] arr = mSelectListPreference.getEntries();
		String value = "";
		for (int i = 0; i < list.size(); i++) {
			value += arr[Integer.parseInt(list.get(i)) - 1];
			if (i < list.size() - 1)
				value += "、";
		}
		mSelectListPreference.setTitle(getString(R.string.alert_day)
				+ value);
		if (firstLaunch)
			PreferenceManager.getDefaultSharedPreferences(this).edit()
					.putBoolean("PH_FIRST_LAUNCH", false).commit();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange
	 * (android.preference.Preference, java.lang.Object)
	 */
	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		String key = preference.getKey();
		if (PH_RING_ONOFF.equals(key)) {

		} else if (PH_VIBRATE_ONOFF.equals(key)) {

		} else if (PH_TIME_PICK.equals(key)) {
			preference.setTitle(getString(R.string.alert_time)
					+ newValue.toString());
		}/* else if (PH_DAY_PICK.equals(key)) {
			@SuppressWarnings("unchecked")
			List<String> list = new ArrayList<String>(
					(HashSet<String>) newValue);
			Collections.sort(list);
			CharSequence[] arr = mSelectListPreference.getEntries();
			String value = "";
			for (int i = 0; i < list.size(); i++) {
				value += arr[Integer.parseInt(list.get(i)) - 1];
				if (i < list.size() - 1)
					value += "、";
			}
			preference.setTitle(getString(R.string.alert_day) + value);
		}*/
		return true;
	}

}
